<template>
  <piece>
    <piece-item
      left-icon="icon-saoma"
      :left-icon-custom="true"
      :content-left="seatNumber ? '重新绑定座位' : '去绑定座位号'"
      :is-content-let-font-weight="seatNumber == null"
      @click="scanShopQR"
    />
  </piece>
</template>

<script setup lang="ts">
import Piece from '@/components/piece/index.vue'
import PieceItem from '@/components/piece/piece-item/index.vue'
import { $modal } from '@/plugins'
import $store from '@/store'
import UrlUtils from '@/utils/UrlUtils'
const props = defineProps<{ shopId: number }>()
const seatNumber = $store.shop.seatNumber.getRef()
// 扫码-店铺座位码
function scanShopQR() {
  uni.scanCode({
    success: function (res) {
      const params = UrlUtils.parseUrlParams(res.path)
      console.log('扫码结果', params, res)
      if (params == null || params.shopId == null) {
        $modal.toastErrorMsg('这不是个有效的店铺餐桌二维码！')
      } else {
        if (parseInt(params.shopId) !== props.shopId) return $modal.toastErrorMsg('不是当前店铺的二维码！')
        // 修改座位号
        seatNumber.value = params.seatNumber
      }
    }
  })
}
// 可被访问数据
defineExpose({
  seatNumber
})
</script>

<style scoped></style>
